﻿using System;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using MapEditor;

namespace lapinou_game2
{
    class Editor
    {
        Map map;
        Chooser chooser;
        Vector2 mapSize;
        ContentManager Content;

        bool first_show = true;

        // Initialize
        public Editor(ContentManager Content, Vector2 mapSize)
        {
            this.Content = Content;
            this.mapSize = mapSize;
        }

        // Update
        public void refreshMap(GameTime gameTime, KeyboardState keyboardstate)
        {
            keyboardstate = Keyboard.GetState();

            if (first_show)
            {
                first_show = false;
                map = new Map();
                map = new Map(mapSize);

                for (int y = 0; y < map.Tiles.Length; y++)
                {
                    for (int x = 0; x < map.Tiles[0].Length; x++)
                    {
                        map.Tiles[y][x] = new Tile("image/empty", new Vector2(x, y));
                    }
                }
                map.LoadContent(Content);
                chooser = new Chooser();
                chooser.LoadContent(Content);
            }

            if (chooser != null && map != null)
            {
                chooser.Update(gameTime, mapSize);
                if (keyboardstate.IsKeyDown(Keys.F1))
                {
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].AssetName = "image/floor";
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].LoadContent(Content);
                }
                else if (keyboardstate.IsKeyDown(Keys.F2))
                {
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].AssetName = "image/ground";
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].LoadContent(Content);
                }
                else if (keyboardstate.IsKeyDown(Keys.F3))
                {
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].AssetName = "image/under";
                    map.Tiles[(int)chooser.Position.Y][(int)chooser.Position.X].LoadContent(Content);
                }
            }
        }

        // Draw
        public void drawMap(SpriteBatch spriteBatch)
        {
            if (map != null)
                map.Draw(spriteBatch);
            if (chooser != null)
                chooser.Draw(spriteBatch);
        }
    }
}
